package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.AlertDataTimeDto;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;


    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override public List<AlertData> pageAlertData(AlertDataDto dto) {
        //获取当前用户id :SecurityUtils.getLoginUser().getUserId()
        //当用户ID是护理员的情况
        if(SecurityUtils.getLoginUser().getUserId()!=1){
            if( SecurityUtils.getLoginUser().getUserId()!=null){
                List<AlertData> list = this.lambdaQuery().eq(StrUtil.isNotBlank(dto.getDeviceName()), AlertData::getDeviceName, dto.getDeviceName())
                        .between(dto.getStartTime() != null && dto.getEndTime() != null, AlertData::getProcessingTime, dto.getStartTime(), dto.getEndTime())
                        .eq(dto.getId() != null, AlertData::getId, dto.getId())
                        .eq(dto.getStatus() != null, AlertData::getStatus, dto.getStatus())
                        .eq(AlertData::getUserId, SecurityUtils.getLoginUser().getUserId())
                        .list();
                return list;
            }else {
                return null;
            }

        }else{
            //当用户ID是管理员的情况
            List<AlertData> list = this.lambdaQuery().eq(StrUtil.isNotBlank(dto.getDeviceName()), AlertData::getDeviceName, dto.getDeviceName())
                    .between(dto.getStartTime() != null && dto.getEndTime() != null, AlertData::getProcessingTime, dto.getStartTime(), dto.getEndTime())
                    .eq(dto.getId() != null, AlertData::getId, dto.getId())
                    .eq(dto.getStatus() != null, AlertData::getStatus, dto.getStatus())
                    .list();
            return list;
        }




    }

    @Override
    public void handleAlertData(AlertDataTimeDto dto) {
        //获取当前用户id
        Long userId = SecurityUtils.getLoginUser().getUserId();
        if(dto.getId()==null){
            throw new BaseException("id为空");
        }
        if (dto.getProcessingResult()==null){
            throw new BaseException("处理结果为空");
        }
        if(dto.getProcessingTime()==null){
            throw new BaseException("处理时间为空");
        }
        //获取这条数据
        AlertData data = this.lambdaQuery().eq(dto.getId() != null, AlertData::getId, dto.getId()).one();

        List<AlertData> list = this.lambdaQuery().eq(data.getCreateTime() != null, AlertData::getCreateTime, data.getCreateTime())
                .eq(data.getIotId() != null, AlertData::getIotId, data.getIotId()).eq(data.getFunctionId()!=null,AlertData::getFunctionId,data.getFunctionId()).list();

        list.forEach(x->{
            x.setStatus(1);
            x.setProcessingResult(dto.getProcessingResult());
            x.setProcessingTime(LocalDateTime.ofInstant(dto.getProcessingTime(), ZoneId.of("Asia/Shanghai")));
            x.setProcessorId(userId);
            x.setProcessorName(SecurityUtils.getLoginUser().getUsername());
            alertDataMapper.updateById(x);
        });



    }
}
